import {
	linkToByCheckTab
} from './publicMethods.js'
import {
	buildUrl
} from './index.js'
import {
	wxScanCode,
	uniCall
} from '@/utils/uniPromise.js'
import Store from '@/store/index.js'
var linkList = [{
		linkUrl: 'link',
		url: '/package/index/commonPage/index',
		arr: [{
			key: 'id',
			value: 'pageCode'
		}, {
			key: 'title',
			value: 'title'
		}] // 实际上，新版title使用的是params里的title
	},
	{
		linkUrl: 'productIndex',
		url: '/package/product/prodSearch/productSearch', // id/level/title
	}, // 常规商品搜索页
	{
		linkUrl: 'seckillIndex',
		url: '/package/product/prodSearch/flashSearch', // id/level/title
	}, // 抢购商品搜索页
	{
		linkUrl: 'productDetail',
		url: '/package/product/local/prod/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 商品详情页
	{
		linkUrl: 'cardRecharge',
		url: '/package/user/cardNum/index'
	}, // 卡号充值
	{
		linkUrl: 'productExchange',
		url: '/package/user/productExchange/index'
	}, // 商品兑换
	{
		linkUrl: 'auctionSearch',
		url: '/package/product/prodSearch/auctionSearch',
	}, // 拍卖搜索页
	{
		linkUrl: 'auctionDetail',
		url: '/package/product/dutchAuction/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 拍卖商品详情
	{
		linkUrl: 'vipSearch',
		url: '/package/product/prodSearch/vipSearch',
	}, // vip产品搜索页
	{
		linkUrl: 'vipProdDetail',
		url: '/package/product/vipProd/detail',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // vip商品详情
	{
		linkUrl: 'groupIndex',
		url: '/package/product/group/index'
	}, // 拼团首页
	{
		linkUrl: 'ticketSearch',
		url: '/package/product/prodSearch/ticketSearch',
	}, // 门票搜索页
	{
		linkUrl: 'ticketDetail',
		url: '/package/product/ticket/prod/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 门票详情页
	{
		linkUrl: 'hotelIndex',
		url: '/package/hotel/index'
	}, // 酒店首页
	{
		linkUrl: 'lineSearch',
		url: '/package/product/prodSearch/lineSearch'
	}, // 线路搜索页
	{
		linkUrl: 'integralIndex',
		url: '/package/integral/index'
	}, // 积分商城首页
	{
		linkUrl: 'bonusShop',
		url: '/package/product/prodSearch/bonusSearch'
	}, // 分红商城
	{
		linkUrl: 'bonusDetail',
		url: '/package/product/bonusProd/detail',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 分红商品详情
	{
		linkUrl: 'brandSearch',
		url: '/package/brand/brandSearch/index'
	}, // 品牌中心
	{
		linkUrl: 'restaurant',
		url: '/package/restaurant/index'
	}, // 餐饮首页
	{
		linkUrl: 'restaurantclass',
		url: '/package/restaurant/take-out/index',
		arr: [{
			key: 'title',
			value: 'title'
		}, {
			key: 'id',
			value: 'id'
		}, {
			key: 'classification_id',
			value: 'classification_id'
		},{
			key: 'meal_id',
			value: 'meal_id'
		}]
	}, //餐饮搜索页
	{
		linkUrl: 'videoIndex',
		url: '/package/video/index'
	}, // 视频产品首页
	{
		linkUrl: 'videoSearch',
		url: '/package/product/prodSearch/videoSearch',
	}, // 视频搜索页
	{
		linkUrl: 'shopList',
		url: '/package/index/shopList/index',
	}, // 供应商店铺列表
	{
		linkUrl: 'category',
		url: 'category'
	}, // tab分类页
	{
		linkUrl: 'release',
		url: '/package/user/release/index',
		arr: [{
			key: 'id',
			value: 'id'
		}]
	}, // 资讯发布
	{
		linkUrl: 'information',
		url: '/package/product/prodSearch/infoSearch'
	}, // 资讯中心
	{
		linkUrl: 'librarySearch',
		url: '/package/product/prodSearch/librarySearch'
	}, // 素材库
	{
		linkUrl: 'infoDetail',
		url: '/package/league/detail/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 资讯详情
	{
		linkUrl: 'Notice',
		url: '/package/notice/index'
	}, // 公告中心
	{
		linkUrl: 'formApply',
		url: '/package/activity/form/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 自定义表单详情
	{
		linkUrl: 'signUp',
		url: '/package/enroll/user/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 报名系统
	{
		linkUrl: 'turntable',
		url: '/package/activity/turntable/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 大转盘抽奖
	{
		linkUrl: 'liveList',
		url: '/package/index/liveList'
	}, // 直播列表
	{
		linkUrl: 'liveDetail',
		url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin'
	}, // 直播间
	{
		linkUrl: 'wxMini',
		method: 'linkUrlToMini'
	}, // 跳转其他小程序
	{
		linkUrl: 'distApply',
		method: 'applyDistributor' // 必须要在jumpApi定义的
	}, // 申请分销商
	{
		linkUrl: 'shareRank',
		method: 'goShareRank' // 必须要在jumpApi定义的
	}, // 申请分销商
	{
		linkUrl: 'webPage',
		method: 'goWebView' // 必须要在jumpApi定义的
	}, // 跳转嵌套页面
	{
		linkUrl: 'businessAddress',
		method: 'businessAddress',
	}, // 商家地址
	{
		linkUrl: 'myCoupon',
		url: '/package/user/coupon/index'
	}, // 我的优惠券
	{
		linkUrl: 'couponPackSearch',
		url: '/package/distribution/coupon/package'
	}, // 优惠券套餐列表页
	{
		linkUrl: 'integral',
		url: '/package/user/sign-in/index'
	}, // 积分签到
	{
		linkUrl: 'auctionVip',
		url: '/package/user/my-vip/index'
	}, // VIP会员
	{
		linkUrl: 'envelope',
		url: '/package/redEnvelopes/index'
		
	}, // 红包
	{
		linkUrl: 'vote',
		url: '/package/activity/vote/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 投票活动
	{
		linkUrl: 'meet',
		url: '/package/meet/index',
		arr: [{
			key: 'id',
			value: 'pk_id'
		}]
	}, // 会议活动
	{
		linkUrl: 'parkIndex',
		url: '/package/dynamicWorld/park-lot/park/index'
	}, // 停车
	{
		linkUrl: 'goScanCode',
		method: 'goScanCode'
	}, // 小程序扫太阳码
	{
		linkUrl: 'goPhone',
		method: 'goPhone'
	}, // 拨打电话
	{
		linkUrl: 'customRoute',
		method: 'customRoute'
	}, // 自定义
	{
		linkUrl: 'tipsModal',
		method: 'tipsModal'
	}, // 弹窗提示

]
var jumpApi = {
	'applyDistributor': applyDistributor,
	'goShareRank': goShareRank,
	'linkUrlToMini': linkUrlToMini,
	'goWebView': goWebView,
	'businessAddress': businessAddress,
	'customRoute': customRoute,
	'tipsModal': tipsModal,
	'goScanCode': goScanCode,
	'goPhone': goPhone
}

// 配置页页面跳转：通过 linkUrl判断
export function pageComponentsLink(item) {
	let linkUrl = item.linkUrl || ''
	let obj = linkList.find((linkItem) => {
		return linkItem.linkUrl === linkUrl
	})
	if (obj) {
		if (obj.method) {
			/**页面跳转的特殊方法 */
			if (item.defaultParams && item.defaultParams.loginAuth && !Store.state.vuex_token) {
				Store.commit('$uStore', {
					name: 'vuex_login',
					value: true
				})
			} else {
				jumpApi[obj.method](item)
			}
			// var func = eval(obj.method)
			// new func(item, obj.routeName)
		} else {
			judgeJump(obj, item)
		}
	} else {
		console.log('没有获取到匹配的跳转数据')
	}
}

/**
 * 配置页页面跳转：通过路由url判断页面跳转
 * {path:''}
 * */
export function pathLink(item) {
	console.log('item',item)
	if (item.path.includes('loginAuth') && !Store.state.vuex_token) {
		Store.commit('$uStore', {
			name: 'vuex_login',
			value: true
		})
	} else if (item.path.includes('appid=')) {
		goMini(item.path)
	} else {
		let str = (item.path || '').split('?')[0]
		if (jumpApi[str]) {
			jumpApi[str](item)
		} else {
			linkToByCheckTab(item.path)
		}
	}
	return
	// let arr = (item.path || '').split('?')
	// let url = arr[0] || ''
	// let arr1 = (arr[1] || '').split('&')
	// let params = {}
	// arr1.forEach((item)=>{
	// 	let arr2 = item.split('=')
	// 	params[arr2[0]] = arr2[1]
	// })
	// item.params = {
	// 	...params
	// }
	// let obj = linkList.find((linkItem) => {
	// 	return linkItem.url === url
	// })
	// if (obj) {
	// 	judgeJump(obj, item)
	// }else{
	// 	// 没有获取到匹配的跳转数据
	// }
}



// 页面跳转普通方法
function judgeJump(obj, item) {
	let targetUrl = obj.url
	if (!targetUrl) {
		return
	}
	let newParams = {}
	if (obj.arr) {
		obj.arr.forEach((arrItem) => {
			if (item[arrItem.value] || item[arrItem.value] === 0) {
				newParams[arrItem.key] = item[arrItem.value]
			}
		})
	}
	if (obj.urlId) {
		/** 如果路由传参方式是：params */
		targetUrl = targetUrl + item[obj.urlId]
	}
	if (item.params) {
		let parObj = item.params
		Object.keys(parObj).forEach((key) => {
			if (parObj[key] || parObj[key] === 0) {
				newParams[key] = parObj[key]
			}
		})
	}
	if (item.defaultParams) {
		let defaultObj = item.defaultParams
		Object.keys(defaultObj).forEach((key) => {
			if (defaultObj[key] || defaultObj[key] === 0) {
				newParams[key] = defaultObj[key]
			}
		})
	}
	let url = buildUrl(targetUrl, newParams)
	if (url.includes('loginAuth') && !Store.state.vuex_token) {
		/*需要登录且没有登录*/
		Store.commit('$uStore', {
			name: 'vuex_login',
			value: true
		})
	} else {
		linkToByCheckTab(url)
	}
}


// 跳转申请分销商
function applyDistributor(item) {
	if (Store.state.vuex_user.type == 3) {
		uni.showModal({
			title: '好物推荐官申请审核中'
		})
	} else {
		let url = '/package/user/user-info/index'
		if (Store.state.vuex_user.type == 1) {
			url += '?type=applyDist'
		}
		linkToByCheckTab(url)
	}
}
// 查看分销商排行榜
function goShareRank(item) {
	let defaultParams = item.defaultParams || {}
	if (defaultParams.allUser || (Store.state.vuex_user.type == 2 || Store.state.vuex_user.type == 4)) {
		linkToByCheckTab(`/package/userSuper/share/rank/index`)
	}
}
// 跳转其他小程序
function linkUrlToMini(item) {
	let str = ''
	let obj = item.params || {}
	str += 'appid=' + obj.appid
	if (obj.miniUrl) {
		str += '&url=' + obj.miniUrl
	}
	goMini(str)
}

function goMini(path) {
	let arr = path.split('&')
	let appid = arr[0].split('=')[1]
	let url = ''
	if (arr[1]) {
		url = arr[1].split('url=')[1]
	}
	uni.navigateToMiniProgram({
		appId: appid,
		path: url,
		success: res => {},
		fail: (err) => {
			console.log('错误', err)
		}
	});
}

// 查看网址
function goWebView(item) {
	let path = ''
	let params = ''
	let query = {}
	if (item.path) {
		// 如：goWebView?https://wwww.baidu.com?id=10
		path = item.path.split('?')[1]
		if (item.path.split('?')[2]) {
			params = item.path.split('?')[2]
		}
	} else {
		let obj = item.params || {}
		path = obj.webUrl.split('?')[0]
		params = obj.webUrl.split('?')[1] || ''
	}
	if (params) {
		params.split('&').map((str) => {
			let objArr = str.split('=')
			query[objArr[0]] = objArr[1]
		})
	}
	let url = `/package/index/webView/index?path=${path}&query=${JSON.stringify(query)}`
	linkToByCheckTab(url)
}

function businessAddress(item) {
	let params = item.params || {}
	if (params.coordinate) {
		let obj = {
			// #ifdef MP-ALIPAY
			name: params.address,
			address: params.address
			// #endif
		};
		let arr = params.coordinate.split(',')
		uni.openLocation({
			latitude: Number(arr[0]),
			longitude: Number(arr[1]),
			...obj
		});
	} else {
		linkToByCheckTab('/package/index/map-navigation/index')
	}
}


function customRoute(item) {
	pathLink(item.params || {})
}

function tipsModal(item) {
	let obj = item.params || {}
	console.log('bo',obj)
	if (obj.type == 1) {
		let modal = {}
		if (obj.title) {
			modal.title = obj.title
		}
		if (obj.content) {
			modal.content = obj.content
		}
		uni.showModal({
			...modal,
			showCancel: false
		})
	} else {
		// 轻提示
		uni.showToast({
			title: obj.title,
			icon: 'none',
		})
	}
}

function goScanCode() {
	wxScanCode({
		onlyFromCamera: true
	}).then((res) => {
		console.log('res',res)
		if (res.path) {
			pathLink({
				path: res.path
			})
		} else {
			uni.showToast({
				icon: 'none',
				title: '无法识别'
			})
		}
	})
}

function goPhone(item) {
	let obj = item.params || {}
	uniCall(obj.phone, '未设置联系方式')
}
